package common;


import common.domain.ExamQuestions;
import common.domain.ExamQuestionsMessage;
import common.domain.Student;
import common.domain.StudentMessage;

import java.io.*;
import java.net.Socket;
import java.util.*;

/**
 * @author LinLe
 * @create 2020-12-23 21:03
 */
//对于对象流进行数据的发送和接收
public class DataExchange {
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    private Socket s;
    
    public DataExchange(Socket s) throws IOException {
        this.s = s;
        init(s);
    }
    
    private void init(Socket s) throws IOException {
        oos = new ObjectOutputStream(s.getOutputStream());
        ois = new ObjectInputStream(s.getInputStream());
    }
    
    public String receiveString() throws IOException, ClassNotFoundException {
//        return br.readLine().trim();
       
       String s =  (String) ois.readObject();
       return s;
    }
    public Student receiveStudent() throws IOException, ClassNotFoundException {
        return (Student) ois.readObject();
        
    }
    public ExamQuestions receiveExamQuestions() throws IOException, ClassNotFoundException {
        return (ExamQuestions) ois.readObject();
    }
    public StudentMessage receiveStudentMessage() throws IOException, ClassNotFoundException {
        return (StudentMessage) ois.readObject();
    }
    public Set<ExamQuestions> receiveSet() throws IOException, ClassNotFoundException {
        return (Set<ExamQuestions>) ois.readObject();
    }
    
    public HashMap<String, Student> receiveCol() throws IOException, ClassNotFoundException {
        return  (HashMap<String, Student>) ois.readObject();
    }
    public void sendString(String success) throws IOException {
        oos.writeObject(success);
        oos.reset();
    }
    public void sendStudent(Student student) throws IOException {
        oos.writeObject(student);
        oos.flush();
    }
    public void sendExamQuestions(ExamQuestions questions) throws IOException {
        oos.writeObject(questions);
        oos.flush();
    }
    public void sendExamQuestionsMessage(ExamQuestionsMessage questionsMessage) throws IOException {
        oos.writeObject(questionsMessage);
        oos.flush();
    }
    public void sendStudentMessage(StudentMessage sm) throws IOException {
        oos.writeObject(sm);
        oos.flush();
    }
    public void sendCol(HashMap<String, Student> stu) throws IOException {
        oos.writeObject(stu);
        oos.flush();
        oos.reset();
    }
    
    public void sendSet(Set<ExamQuestions> examQuestions) throws IOException {
        oos.writeObject(examQuestions);
        oos.flush();
        oos.reset();
    }
    
    public void close()  {
        try {
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
